package com.appsindy.reserv.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.appsindy.common.AppsindyConstants;
import com.appsindy.common.action.AppsindyBaseAction;
import com.appsindy.common.bo.GlobalUser;
import com.appsindy.common.dao.GlobalUserDAO;
import com.appsindy.common.util.DateUtils;
import com.appsindy.reserv.ReservConstants;
import com.appsindy.reserv.bo.Counter;
import com.appsindy.reserv.bo.ShiftControlMap;
import com.appsindy.reserv.bo.ShiftTrans;
import com.appsindy.reserv.bo.ShiftTransDt;
import com.appsindy.reserv.bo.ShiftTransDtMap;
import com.appsindy.reserv.dao.ShiftControlDAO;

public class FirstPageAction extends AppsindyBaseAction {
	
	private ShiftControlDAO shiftControlDAO;
	private GlobalUserDAO globalUserDAO;

	private ShiftTrans shiftTrans;
	private List<ShiftTransDt> shiftTransDt;
	private int shiftNo;
	
	
	public String execute() {
		HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
		httpSession.setAttribute("S_SHIFT_LOGIN_DLG", "N");
		return SUCCESS;
	}
	
	public String shiftReserv(){
		HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
		httpSession.setAttribute("S_MENU_TYPE", "RESERV");
		String allowStatus = (String)httpSession.getAttribute("S_ALLOW_SHIFT");
		if(allowStatus != null && "Y".equals(allowStatus)){
			httpSession.setAttribute("S_SHIFT_LOGIN_DLG", "N");
			return "doReserv";
		} else{
			httpSession.setAttribute("S_ALLOW_SHIFT", "N");
			httpSession.setAttribute("S_SHIFT_LOGIN_DLG", "Y");
			return SUCCESS;
		}
    }
	
	public String shiftCtrl(){
		HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
		httpSession.setAttribute("S_MENU_TYPE", "SHFCTRL");
		String allowStatus = (String)httpSession.getAttribute("S_ALLOW_SHIFT");
		if(allowStatus != null && "Y".equals(allowStatus)){
			httpSession.setAttribute("S_SHIFT_LOGIN_DLG", "N");
			return "doShift";
		} else{
			httpSession.setAttribute("S_ALLOW_SHIFT", "N");
			httpSession.setAttribute("S_SHIFT_LOGIN_DLG", "Y");
			return SUCCESS;
		}
    }
	
	public String login() {
    	HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
    	GlobalUser globalUser = (GlobalUser)httpSession.getAttribute(AppsindyConstants.S_USER_LOGIN);
    	Counter counter = (Counter)httpSession.getAttribute(AppsindyConstants.S_COUNTER);

		log.info("login executeShiftControl : globalUser.getUserId() = " + globalUser.getUserId());
		
		ShiftControlMap parameterMap = new ShiftControlMap();
		parameterMap.setUserLoginId(globalUser.getUserId());
		parameterMap.setOrgId(globalUser.getOrganizationId());
		parameterMap.setShiftStatusOpen(ReservConstants.ShiftStatusOpen);
		parameterMap.setCounterId(counter.getCounterId());
		try{
			getShiftControlDAO().executeShiftControl(parameterMap);
		} catch(Exception e){
			addActionError("Have an error : " + e.getMessage());
            return execute();
		}
		log.info("login executeShiftControl : ShiftTransId = " + parameterMap.getShiftTransId());
		log.info("login executeShiftControl : UserId = " + parameterMap.getUserId());
		log.info("login executeShiftControl : ShiftNumOut = " + parameterMap.getShiftNumOut());
		log.info("login executeShiftControl : NoOpenFlagOut = " + parameterMap.getNoOpenFlagOut());
		log.info("login executeShiftControl : ErrorFlagOut = " + parameterMap.getErrorFlagOut());
		
		String json = null;
		if("Y".equals(parameterMap.getErrorFlagOut())){	// Error
			String userShowName = "";
			if(parameterMap.getUserId() > 0){
				userShowName = getGlobalUserDAO().getUserShowName(parameterMap.getUserId());
			}
			httpSession.setAttribute("S_SHIFT_ERROR", "Y");
			
			addActionError("User \'" + userShowName + "\' is not closed shift. You cannot work now.");
			json = "{'status':'Error'" + ", 'errDesc':'User " + userShowName + " is not closed shift. You cannot work now.'}";
			
		} else{		// Success
			httpSession.setAttribute("S_SHIFT_ERROR", "N");
			if("Y".equals(parameterMap.getNoOpenFlagOut())){
				json = "{'status':'Confirm'" + "}";
		        httpSession.setAttribute(AppsindyConstants.S_SHIFT_NUM, (parameterMap.getShiftNumOut()+1));
			} else{
				log.info("Success :: New ShiftNo = " + parameterMap.getShiftNumOut());
				json = "{'status':'Success', 'shiftNo':'" + parameterMap.getShiftNumOut() + "'}";
				
				httpSession.setAttribute("S_ALLOW_SHIFT", "Y");
		        httpSession.setAttribute(AppsindyConstants.S_SHIFT_TRANS_ID, parameterMap.getShiftTransId());
		        httpSession.setAttribute(AppsindyConstants.S_SHIFT_NUM, parameterMap.getShiftNumOut());
			}
		}
        httpSession.setAttribute("S_SHIFT_LOGIN_DLG", "N");
        
        setJsonModel(json);
        return JSON_RESULT;
    }
	
	public String openShift(){
    	HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
		GlobalUser user = (GlobalUser)httpSession.getAttribute(AppsindyConstants.S_USER_LOGIN);
		Counter counter = (Counter)httpSession.getAttribute(AppsindyConstants.S_COUNTER);
		try{
			if(getShiftNo() > -1){
				ShiftTrans st = new ShiftTrans();
				st.setUserId(user.getUserId());
				st.setOrganizationId(user.getOrganizationId());
				st.setShiftStartDatetime(new Date());
				st.setShiftEndDatetime(null);
				st.setShiftNum(getShiftNo());
				st.setShiftStatus(ReservConstants.ShiftStatusOpen);
				st.setCounterId(counter.getCounterId());
				
				getShiftControlDAO().insertShiftTrans(st);
				log.info("[Header] shiftTransId = " + st.getShiftTransId());
				
				if(st.getShiftTransId() > 0){
					ShiftTransDtMap std = new ShiftTransDtMap();
					std.setUserId(user.getUserId());
					std.setOrgId(user.getOrganizationId());
					std.setShiftTransId(st.getShiftTransId());
					std.setLineNum(1);
					byte[] utf8 = ReservConstants.BalanceForward.getBytes("UTF-8");
					std.setTransName(new String(utf8, "UTF-8"));
					std.setTransDesc(null);
					std.setTransDateTime(DateUtils.getMySqlDateTimeStr(new Date(), false));
					std.setShiftTransType(ReservConstants.GlobalTypeShiftTransType);
					std.setShiftTransTypeIn(ReservConstants.ShiftTransTypeIn);
					std.setShiftTransSource(ReservConstants.GlobalTypeShiftTransSource);
					std.setShiftTransSourceSystem(ReservConstants.ShiftTransSourceSystem);
					
					getShiftControlDAO().insertShiftTransDetail(std);
					log.info("[Detail] shiftTransDetailId = " + std.getShiftTransDtId());
				}
				
				ShiftTrans shiftTrx = getShiftControlDAO().selectShiftTransView(st.getShiftTransId());
				if(shiftTrx != null){
					setShiftTrans(shiftTrx);
				} else{
					throw new Exception("No data for ShiftTransId : " + st.getShiftTransId());
				}
				
				List<ShiftTransDt> shiftTrxDtList = getShiftControlDAO().selectShiftTransDetailView(st.getShiftTransId());
				if(shiftTrxDtList != null){
					setShiftTransDt(shiftTrxDtList);
				} else{
					setShiftTransDt(new ArrayList<ShiftTransDt>());
				}
			}
			httpSession.setAttribute("S_ALLOW_SHIFT", "Y");
			httpSession.setAttribute("S_ALLOW_SHIFT_MASTER", getShiftTrans());
			httpSession.setAttribute("S_ALLOW_SHIFT_DETAIL", getShiftTransDt());
			
		} catch(Exception e){
			httpSession.setAttribute("S_ALLOW_SHIFT", "N");
			e.printStackTrace();
		}
		return "shiftControlPage";
	}
	
	public void setShiftControlDAO(ShiftControlDAO shiftControlDAO){
		this.shiftControlDAO = shiftControlDAO;
	}
	public ShiftControlDAO getShiftControlDAO(){
		return shiftControlDAO;
	}
	
	public void setGlobalUserDAO(GlobalUserDAO globalUserDAO){
		this.globalUserDAO = globalUserDAO;
	}
	public GlobalUserDAO getGlobalUserDAO(){
		return globalUserDAO;
	}

	public int getShiftNo() {
		return shiftNo;
	}
	public void setShiftNo(int shiftNo) {
		this.shiftNo = shiftNo;
	}

	public ShiftTrans getShiftTrans() {
		return shiftTrans;
	}
	public void setShiftTrans(ShiftTrans shiftTrans) {
		this.shiftTrans = shiftTrans;
	}
	
	public List<ShiftTransDt> getShiftTransDt() {
		return shiftTransDt;
	}
	public void setShiftTransDt(List<ShiftTransDt> shiftTransDt) {
		this.shiftTransDt = shiftTransDt;
	}
	
}
